package com.ad.pet.app.shopcar;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;
import com.ad.pet.models.remote.collect.BigArticle;

import javax.inject.Inject;


public class YouLikePresenter extends AbsBasePresenter implements YouLikeContract.Presenter{


    private ApiService mApiService;
    private YouLikeContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;


    @Override
    public void onCreate(YouLikeContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }


    @Override
    public void getArticleList(String classId, String page) {
        mRxDisposable.add(mApiService.youlike(classId,page)
                .subscribe(new AppSubscriber<BigArticle>(mView) {
                    @Override
                    public void next(BigArticle list) {
                        mView.getArticleListSuccess(list);
                    }
                }));
    }

    @Override
    public void articleComment(String articleId, String text) {
        mRxDisposable.add(mApiService.articleComment(articleId,text)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.articleCommentSuccess();
                    }
                }));
    }

    @Override
    public void getCollectArticleList(String page) {
        mRxDisposable.add(mApiService.getCollectArticleList(page)
                .subscribe(new AppSubscriber<BigArticle>(mView) {
                    @Override
                    public void next(BigArticle list) {
                        mView.getCollectArticleListSuccess(list);
                    }
                }));
    }

    @Override
    public void collectArticle(String articleId) {
        mRxDisposable.add(mApiService.collectArticle(articleId)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.collectArticleSuccess();
                    }
                }));
    }

    @Override
    public void cancelCollectArticle(String articleId) {
        mRxDisposable.add(mApiService.cancelCollectArticle(articleId)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User list) {
                        mView.cancelCollectArticleSuccess();
                    }
                }));
    }

    @Override
    public void zanArticle(String articleId) {
        mRxDisposable.add(mApiService.zanArticle(articleId)
                .subscribe(new AppSubscriber<String>(mView) {
                    @Override
                    public void next(String list) {
                        mView.zanArticletSuccess();
                    }
                }));
    }

    @Override
    public void cancelZanArticle(String articleId) {
        mRxDisposable.add(mApiService.cancelZanArticle(articleId)
                .subscribe(new AppSubscriber<String>(mView) {
                    @Override
                    public void next(String list) {
                        mView.cancelZanArticleSuccess();
                    }
                }));
    }


    @Inject
    public YouLikePresenter(ApiService apiService,
                               Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

}
